/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.recitation.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.poemmanager.domain.Comment;
import me.zhengjie.recitation.domain.RecitationComment;
import me.zhengjie.recitation.domain.vo.RecitationCommentQueryCriteria;
import me.zhengjie.recitation.mapper.RecitationCommentMapper;
import me.zhengjie.recitation.service.RecitationCommentService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author yellow
* @date 2025-04-20
**/
@Service
@RequiredArgsConstructor
public class RecitationCommentServiceImpl extends ServiceImpl<RecitationCommentMapper, RecitationComment> implements RecitationCommentService {

    private final RecitationCommentMapper recitationCommentMapper;

    @Override
    public PageResult<RecitationComment> queryAll(RecitationCommentQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(recitationCommentMapper.findAll(criteria, page));
    }

    @Override
    public List<RecitationComment> queryAll(RecitationCommentQueryCriteria criteria){
        return recitationCommentMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(RecitationComment resources) {
        recitationCommentMapper.addRecitationCommentCount(resources.getObjectId());
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecitationComment resources) {
        RecitationComment recitationComment = getById(resources.getCommentId());
        recitationComment.copy(resources);
        saveOrUpdate(recitationComment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        for(Integer id : ids){
            RecitationComment recitationComment = recitationCommentMapper.selectById(id);
            recitationCommentMapper.subRecitationCommentCount(recitationComment.getObjectId());
        }
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<RecitationComment> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecitationComment recitationComment : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("评论的朗读的主键id", recitationComment.getObjectId());
            map.put("用户名", recitationComment.getUserName());
            map.put("评论内容", recitationComment.getContent());
            map.put("创建日期", recitationComment.getCreatedAt());
            map.put("更新时间", recitationComment.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Comment> getMyComment(String userName) {
        List<Comment> cs = recitationCommentMapper.getMyComment(userName);
        for(Comment c : cs){
            c.setTitle(recitationCommentMapper.getRecitationTitle(c.getObjectId()));
            c.setObjectType(3);
        }
        return List.of();
    }
}